package com.qianfeng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.MoneyHelper;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.ChargeProject;
import com.qianfeng.entity.ChargeProjectRegistration;
import com.qianfeng.entity.InHospital;
import com.qianfeng.mapper.ChargeProjectMapper;
import com.qianfeng.mapper.ChargeProjectRegistrationMapper;
import com.qianfeng.mapper.InHospitalMapper;
import com.qianfeng.service.MoneyHelperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class MoneyHelperServiceImpl implements MoneyHelperService {

    @Autowired
    private InHospitalMapper inHospitalMapper;
    @Autowired
    private ChargeProjectMapper chargeProjectMapper;
    @Autowired
    private ChargeProjectRegistrationMapper chargeProjectRegistrationMapper;

    @Override
    public Page getMoneyHelperPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<InHospital> inHospitalList = inHospitalMapper.getInHospitalList();
        PageInfo<InHospital> pageInfo = new PageInfo<>(inHospitalList);
        page.setPageCount(pageInfo.getPages());
        page.setHasNext(pageInfo.isHasNextPage());

        List<MoneyHelper> moneyHelpers = new ArrayList<>();

        for (InHospital inHospital : inHospitalList) {
            List<ChargeProjectRegistration> chargeProjectRegistrationList = chargeProjectRegistrationMapper.getChargeProjectRegistrationList(inHospital.getrId());

            double my = 0d;
            //计算收费项
            for (ChargeProjectRegistration chargeProjectRegistration : chargeProjectRegistrationList) {
                ChargeProject project = chargeProjectMapper.getChargeProjectById(chargeProjectRegistration.getCpId());
                my+=project.getCpMoney();
            }

            //押金
            Double ihMoney = inHospital.getIhMoney();
            //余额
            double d = ihMoney-my;
            MoneyHelper moneyHelper = new MoneyHelper();
            moneyHelper.setInHospital(inHospital);
            moneyHelper.setMoney(d);
            moneyHelpers.add(moneyHelper);
            page.setObj(moneyHelpers);
            System.out.println(ihMoney);
            System.out.println(my);

        }
        return page;
    }
}
